<?php 
namespace app\admin\controller;

use think\Db;

class Goodsadvise extends Base{
	public function index(){
		$list = Db::field('pg.pg_id,pg.goods_id,pg.brand_name,pg.specval_name,pg.goods_name,pg.specval_id,sval.price,pg.picture,p.employee_name,pg.create_time,pg.cur_price as purchase_price')
		->table('sdzx_goods_specval')->alias('sval')
		->join('sdzx_purchase_goods pg','sval.specval_id=pg.specval_id')
		->join('sdzx_purchase p','pg.purchase_id=p.purchase_id')
		->group('sval.specval_id')
		->order('pg.create_time')
		->where('pg.apply_price=1 and pg.cur_price!=sval.price and pg.cur_price!=0')
		->select();

		return $this->fetch('',['list'=>$list]);
	}
	
	public function change(){
		$data = request()->only('specval_id,purchase_price,pg_id');
		!$data && $this->error('参数错误');
		
		$res = db('goods_specval')->where(['specval_id' => $data['specval_id']])->update(['price' => $data['purchase_price']]);
		if ($res === false) {
			$this->error('操作失败','');
		}
		$re = db('purchase_goods')->where(['pg_id'=>$data['pg_id']])->update(['apply_price'=>2]);
		if($re === false){
			$this->error('操作失败','');
		}

		$this->success('操作成功','');
	}

	public function ignore(){
		$data = request()->only('pg_id');
		!$data && $this->error('参数错误');
		

		$re = db('purchase_goods')->where(['pg_id'=>$data['pg_id']])->update(['apply_price'=>-1]);
		if($re === false){
			$this->error('操作失败','');
		}

		$this->success('操作成功','');
	}
}
?>